﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmDTXetluuban : Form
    {
        SqlConnection connection = new SqlConnection("Data Source = .; Initial Catalog = SM; Integrated Security = True");
        int i = 0;
        public frmDTXetluuban()
        {
            InitializeComponent();
            groupControl2.Visible = false;
        }

        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql,connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message);
            }
            return ds;
        }

        private void frmDTXetluuban_Load(object sender, EventArgs e)
        {
            DataSet ds = FillDataset("Select [MalopID] ,[Tenlop] from [T03_Lop_hoc]");
            lkuMalop.Properties.DataSource = ds.Tables[0];
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                DataSet ds = FillDataset("exec PC_XetLuuBan N'" + lkuMalop.Text + "', N'" + txtNamhoc.Text + "'");
                if (ds.Tables[0].Rows.Count == 0)
                    MessageBox.Show("Không tìm thấy dữ liệu");
                else
                {
                    groupControl2.Visible = true;
                    gridControl1.DataSource = ds.Tables[0];
                }
            }
            else
                MessageBox.Show("Bạn chưa nhập dữ liệu cần thiết", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.None);
       
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
                i = 0;
            else if (radioGroup1.SelectedIndex == 1)
                i = 1;
            else if (radioGroup1.SelectedIndex == 2)
                i = 2;
            else
                i = 3;
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            string sql;
            if (i == 0)
            {
                sql = "exec PC_XetLuuBan N'" + lkuMalop.Text + "', N'" + txtNamhoc.Text + "'";
                rpt_LL_LB_TH rpt_tonghop = new rpt_LL_LB_TH();
                rpt_tonghop.DataSource = FillDataset(sql);
                rpt_tonghop.BindData(lkuMalop.Text, txtNamhoc.Text);
                rpt_tonghop.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
                rpt_tonghop.ShowPreview();
            }
            else if (i == 1)
            {
                sql = "PC_InXetLuuBan N'" + lkuMalop.Text + "', N'" + txtNamhoc.Text + "', N'" + "LL" + "'";
                rptDSSVLenlop rpt_LL = new rptDSSVLenlop();
                rpt_LL.DataSource = FillDataset(sql);
                rpt_LL.BindData(lkuMalop.Text, txtNamhoc.Text);
                rpt_LL.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
                rpt_LL.ShowPreview();
            }
            else if (i == 2)
            {
                sql = "PC_InXetLuuBan N'" + lkuMalop.Text + "', N'" + txtNamhoc.Text + "', N'" + "LB" + "'";                
                rptDSSVLuuban rpt_LB = new rptDSSVLuuban();
                rpt_LB.DataSource = FillDataset(sql);
                rpt_LB.BindData(lkuMalop.Text, txtNamhoc.Text);
                rpt_LB.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
                rpt_LB.ShowPreview();
            }
            else
            {
                sql = "PC_InXetLuuBan N'" + lkuMalop.Text + "', N'" + txtNamhoc.Text + "', N'" + "TH" + "'";
                rptDSSVThoihoc rpt_TH = new rptDSSVThoihoc();
                rpt_TH.DataSource = FillDataset(sql);
                rpt_TH.BindData(lkuMalop.Text, txtNamhoc.Text);
                rpt_TH.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
                rpt_TH.ShowPreview();
            }
            
            
        }
    }
}
